﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Classes.util.*;
    import __AS3__.vec.*;
    import Classes.effects.*;
    import Display.*;
    import GameMode.*;
    import Modules.storage.*;
    import Classes.gates.*;
    import Modules.crew.*;
    import Modules.storage.ui.*;

    public class ItemStorage extends Decoration {

        private var m_gate:IGate;
        private var m_gateFactory:GateFactory;
        private var m_overrideGameMode:GameMode;

        public function ItemStorage(_arg1:String=null){
            super(_arg1);
            this.m_gateFactory = new GateFactory();
            this.m_gateFactory.register(GateType.CREW, CrewGate);
            this.m_overrideGameMode = null;
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.m_gate = this.m_gateFactory.loadGateFromObject(_arg1, getItem(), this, null);
        }
        override public function onBuildingConstructionCompleted_PreServerUpdate():void{
            super.onBuildingConstructionCompleted_PreServerUpdate();
            var _local1:BaseStorageUnit = this.initStorageUnitFromItem();
            this.m_gate = this.m_gateFactory.loadGateFromXML(getItem(), this, "storage", null);
        }
        override public function onAddCrewMember():void{
            var _local1:BaseStorageUnit = Global.player.storageComponent.getStorageUnit(this.getStorageType(), this.getStorageKey());
            _local1.capacity = this.getStorageCapacity();
            refreshArrow();
        }
        override public function getToolTipHeader():String{
            if (UI.isViewOpen(StorageView)){
                return ("");
            };
            return (super.getToolTipHeader());
        }
        override public function getToolTipAction():String{
            var _local1:String;
            if (UI.isViewOpen(StorageView)){
                return ("");
            };
            if (!Global.isVisiting()){
                if (isUpgradePossible()){
                    _local1 = ZLoc.t("Dialogs", "UpgradeToolTip");
                } else {
                    _local1 = super.getToolTipAction();
                    if ((((_local1 == null)) || ((_local1 == "")))){
                        _local1 = ZLoc.t("Items", "warehouse_defaultToolTip");
                    };
                };
            };
            return (_local1);
        }
        override public function onPlayAction():void{
            super.onPlayAction();
            if (!Global.isVisiting()){
                if (isUpgradePossible()){
                    upgradeBuildingIfPossible();
                } else {
                    UI.displayItemStorageDialog(this);
                };
            };
        }
        override protected function updateArrow():void{
            if (isUpgradePossible()){
                this.createStagePickEffect();
            };
        }
        override protected function createStagePickEffect():void{
            EffectsUtil.createStagePickEffect(StagePickEffect.PICK_UPGRADE, this);
        }
        override public function sell():void{
            if (this.canFinishSell()){
                super.sell();
            } else {
                UI.displayMessage(ZLoc.t("Main", "CannotSellStorageItem", {item:getItemFriendlyName()}));
            };
        }
        override public function onSell():void{
            super.onSell();
            Global.player.storageComponent.removeStorageUnit(this.getStorageType(), this.getStorageKey());
        }
        private function canFinishSell():Boolean{
            var _local1:Boolean;
            var _local2:BaseStorageUnit = Global.player.storageComponent.getStorageUnit(this.getStorageType(), this.getStorageKey());
            if (((_local2) && (_local2.size))){
                _local1 = false;
            };
            return (_local1);
        }
        override public function sendToInventory():void{
            super.sendToInventory();
            Global.player.storageComponent.removeStorageUnit(this.getStorageType(), this.getStorageKey());
        }
        override public function onUpgrade(_arg1:Item, _arg2:Item, _arg3:Boolean=true):void{
            var _local6:Vector.<ItemInstance>;
            var _local7:ItemInstance;
            super.onUpgrade(_arg1, _arg2);
            var _local4:BaseStorageUnit = Global.player.storageComponent.getStorageUnit(StorageType.createEnum(_arg1.storageType), _arg1.storageKey);
            var _local5:BaseStorageUnit = Global.player.storageComponent.getStorageUnit(StorageType.createEnum(_arg2.storageType), _arg2.storageKey);
            if (_local5 == null){
                _local5 = this.initStorageUnitFromItem(_local4.capacity);
            };
            if (_local4 != _local5){
                _local6 = _local4.getAllItems();
                for each (_local7 in _local6) {
                    _local4.remove(_local7);
                    _local5.add(_local7);
                };
                Global.player.storageComponent.removeStorageUnit(StorageType.createEnum(_arg1.storageType), _arg1.storageKey);
            };
            this.m_gate = this.m_gateFactory.loadGateFromXML(getItem(), this, "storage", null);
            _local5.capacity = this.getStorageCapacity();
        }
        private function getStorageCapacity():int{
            var _local1:int = this.getInitialCapacity();
            var _local2:Crew = Global.crews.getCrewById(getId());
            var _local3:int = (_local2) ? _local2.count : 0;
            return (((_local1 + _local3) + Global.player.additionalWareHouseSlots));
        }
        public function getStorageType():StorageType{
            return (StorageType.createEnum(m_item.storageType));
        }
        public function getStorageKey():String{
            return (m_item.storageKey);
        }
        public function getInitialCapacity():int{
            return (m_item.storageInitCapacity);
        }
        public function getMaxCapacity():int{
            return ((m_item.storageMaxCapacity + Global.player.additionalWareHouseSlots));
        }
        public function getGate():IGate{
            return (this.m_gate);
        }
        protected function initStorageUnitFromItem(_arg1:int=0):BaseStorageUnit{
            var _local2:BaseStorageUnit = Global.player.storageComponent.addStorageUnit(this.getStorageType(), this.getStorageKey());
            _local2.maxCapacity = this.getMaxCapacity();
            if (_arg1 > 0){
                _local2.capacity = _arg1;
            } else {
                _local2.capacity = this.getStorageCapacity();
            };
            return (_local2);
        }

    }
}//package Classes 
